/**
 * 簿记详情
 * zhanghaitao
 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import EftBuySellC from '../../InvestmentManage/EtfTradeNew/orderComps/eftBuySell';
import BondTradeDetailC from '../../InvestmentManage/BondTradeNew/orderComps/bondTradeDetail';
import FinaFlockDetailC from '../../InvestmentManage/finaFlockingNew/orderComps/orderFormC';
import NonstandardOrderDetailC from '../../InvestmentManage/nonstandardOrder/orderComps/orderFormC';
import InterBank from '../../InvestmentManage/BuybackOrder/interBank';
import BuyBackOrderExchangePledge from '../../InvestmentManage/BuybackOrder/exchangePledge/baseInfoC';
import BuyBackContractC from '../../InvestmentManage/Trade/BuyBackContract';
import FundTradeDetail from '../../InvestmentManage/fundTradeOrder/common/tradeElements';
import FundTradeEdit from '../../InvestmentManage/fundTradeOrder/AddTradeC';
import DepositOrderDetail from '../../InvestmentManage/depositOrderNew/DepositOrderDetail';
import InstructionSend from '../../InvestmentManage/StockOrder/InstructionSend';
import BondLend from '../TradeBook/BondLending/common/index';
import BondOffLine from '../../InvestmentManage/bondOffline/orderComps/addOrder';
import HKdetail from '../../InvestmentManage/HKstock/orderComps/orderFormC';
import OuterDetail from '../../InvestmentManage/OutSourcingOrder/components/OrderFormC';
import AssetsOrderDetail from '../../InvestmentManage/assetsOrder/orderComps/orderFormC';
import StockIndexFuturesDetail from '../../InvestmentManage/StockIndexFutures/orderComps/orderFormC';
import 'moment/locale/zh-cn';
import { ModalC, SecondHeaderC, DATA_ENTRY, FormGroup } from '../../../comps';
import typeOfTradeX from './typeOfTradeX';

class Detail extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      infos: {},
      type: '',
      info: {},
      timeStamp: undefined,
      infoDetail: {},
      instructType: '',
      clinchInfromation: undefined, //风控信息
    };
  }
  componentDidMount() {
    this.loadValue(this.props.detailObj);
    setTimeout(() => {
      this.setState({
        timeStamp: new Date().getTime(),
      });
    }, 100);
    if (this.props.detailObj.type == '19') {
      //港股需要展示成交信息
      this.props.dispatch({
        type: 'bookkeepingSettleM/clinchInfromation',
        payload: { id: this.props.detailObj.id },
        callback: (data) => {
          if (data) {
            this.setState({
              clinchInfromation: data,
            });
          } else {
            this.setState({
              clinchInfromation: undefined,
            });
          }
        },
      });
    }
  }
  loadValue = (record) => {
    if (record) {
      let trade = typeOfTradeX(record);
      if (trade) {
        if (record.type == 8) {
          this.setState({
            infoDetail: {
              tradeInstruction: {
                id: record.id,
              },
              ...trade,
              combCode: record.portfolioInfoId, //产品编号
              fundCode: record.productId, //组合编号
            },
          });
          if (trade.orderSize == 'F') {
            this.setState({
              instructType: '1',
            });
          } else if (trade.orderSize == 'i') {
            this.setState({
              instructType: '2',
            });
          } else {
            this.setState({
              instructType: '3',
            });
          }
        }
        if (record.type == 21 || record.type == 20) {
          trade.investmentType = record.investmentType;
        }
        this.setState({
          infos: trade,
          info: record,
          type: record.type,
        });
      }
    }
  };
  //交收
  orderInstruction = () => {
    const { info } = this.state;
    //交收
    this.props.dispatch({
      type: 'bookkeepingSettleM/getTradeInstruction',
      payload: {
        id: info.id,
      },
      callback: (res) => {
        if (res === 'success') {
          this.props.changeVisible(false);
          this.props.refresh();
        }
      },
    });
  };
  //取消
  onCancel = (type) => {
    this.props.changeVisible(false);
    if (type == 'refresh') {
      //弹窗有修改操作的页面 列表要需要刷新
      this.props.refresh();
    }
  };
  // 簿记管理 详情以外的 操作按钮功能  基金的确定 网下申购的中签
  editClick = () => {
    const { type } = this.state;
    if (type == '5') this.FundTrade.confirmOk(); //基金的确定按钮
    if (type == '17') {
      //网下申购 中签的确定按钮
      this.BondOffLine.onOk();
    }
  };
  //编辑状态下  模态框展示的标题
  showTitle = () => {
    const { type } = this.state;
    if (type == '5') return '确定';
    if (type == '17') return '中签';
  };
  showTable = () => {
    const { type, info, infos, infoDetail, instructType } = this.state;
    switch (type) {
      // 交易所现券
      case '0':
        return <BondTradeDetailC type={3} info={info ? info : {}} />;
      // 银行间现券
      case '1':
        return <BondTradeDetailC type={3} info={info ? info : {}} />;
      // 上交所固守
      case '2':
        return <BondTradeDetailC type={3} info={info ? info : {}} />;
      // 协商成交
      case '3':
        return <BondTradeDetailC type={3} info={info ? info : {}} />;
      //银行间质押回购
      case '4':
        return <InterBank type={3} info={info ? info : {}} />;
      // 银行间买断式和银行间质押回购详情展示一样  32/33
      case '32':
        return <InterBank type={3} info={info ? info : {}} />;
      case '33':
        return <InterBank type={3} info={info ? info : {}} />;
      //场外基金 若未确认过，根据不同的【交易方向】展示的与簿记下达的字段保持一致，数据需要进行回填，且不可编辑；
      // 若确认过，根据不同的【交易方向】展示与“确认”弹出的模态框内容保持一致，数据需要回填，且不可编辑。
      case '5':
        return (
          <div style={{ height:info?.trade5?.applyMethod=='0'?"400px": '350px' }}>
            {info && info.isEdit ? (
              <FundTradeEdit onCancel={this.onCancel} type='confirmEdit' info={info ? info : {}} onRef={(ref) => (this.FundTrade = ref)} />
            ) : (
              <FundTradeDetail type={['7', '8'].includes(info?.instructionStatus)||info?.trade5?.confirm==1 ? 'confirmView' : '3'} info={info ? info : {}} />
            )}
          </div>
        );
      //理财直融
      case '6':
        return <FinaFlockDetailC type={3} info={info ? infos : {}} />;
      //交易所质押式回购
      case '7':
        return <BuyBackOrderExchangePledge type={3} info={info ? info : {}} />;
      //存款
      case '8':
        return <DepositOrderDetail instructType={instructType} infoDetail={infoDetail} state={5} />;
      //etf指令
      case '9':
        return <EftBuySellC type={3} info={infos ? infos : {}} />;
      //非标
      case '12':
        return <NonstandardOrderDetailC type={3} info={info ? infos : {}} />;
      //交易所协议式回购
      case '13':
        return <BuyBackContractC type={3} tradeInfo={info ? info : {}} timeStamp={this.state.timeStamp} />;
      //股票
      case '15':
        return <InstructionSend type='5' initValue={info ? info : {}} edit={false} onRef={(ref) => (this.Instructionsend = ref)} />;
      //债券借贷
      case '16':
        return <BondLend type='3' initValue={infos ? infos : {}} />;
      //债券网下申购
      case '17':
        return <BondOffLine type={info && info.isEdit ? '5' : '3'} onCancel={this.onCancel} onRef={(ref) => (this.BondOffLine = ref)} info={info ? info : {}} />;
      //港股通
      case '19':
        return <HKdetail type={'3.2'} onCancel={this.onCancel} info={info ? infos : {}} />;
      //资产管理计划指令
      case '20':
        return <AssetsOrderDetail type={'3'} onCancel={this.onCancel} info={info ? infos : {}} />;
      //委外指令
      case '21':
        return <OuterDetail type={'3'} onCancel={this.onCancel} info={info ? infos : {}} />;
      //股指期货 占用两个23、24
      case '23':
        return <StockIndexFuturesDetail type={'3'} onCancel={this.onCancel} info={info ? infos : {}} />;
      //股指期货 占用两个23、24
      case '24':
        return <StockIndexFuturesDetail type={'3'} onCancel={this.onCancel} info={info ? infos : {}} />;
      // 大宗现券
      case '25':
        return <BondTradeDetailC type={3} info={info ? info : {}} />;
    }
  };
  //关闭模态框
  addanCancel = () => {
    this.props.changeVisible(false);
  };
  //getLoading
  getLoading = () => {
    const { type } = this.state;
    if (type == '17') {
      return 'bondOfflineM/luckyNumber';
    }
    if (type == '5') {
      return 'newFundTrade/confirmEdit';
    }
    if (!this.props.visible) {
      return 'bookkeepingSettleM/getTradeInstruction';
    }
    return '';
  };
  //港股成交信息
  clinchTable = () => {
    const { clinchInfromation } = this.state;
    return (
      <div style={{ marginTop: '10px' }}>
        <SecondHeaderC title='成交信息' />
        <div style={{ paddingRight: '55px' }}>
          <FormGroup
            dataSource={[
              {
                dataIndex: 'costAverage',
                title: '成交均价',
                type: DATA_ENTRY.INPUTNUMBER,
                disabled: true,
                initialValue: clinchInfromation?.costAverage,
                precision: 4,
                unit: '元',
                splitStyle: true,
              },
              {
                dataIndex: 'turnoverNum',
                title: '成交数量',
                type: DATA_ENTRY.INPUTNUMBER,
                disabled: true,
                initialValue: clinchInfromation?.turnoverNum,
                precision: 4,
                unit: '股',
                splitStyle: true,
              },
              {
                dataIndex: 'turnoverMoney',
                title: '成交金额',
                type: DATA_ENTRY.INPUTNUMBER,
                disabled: true,
                initialValue: clinchInfromation?.turnoverMoney,
                precision: 4,
                unit: '元',
                splitStyle: true,
              },
            ]}
            rowNumber={3}
            autoLayout={false}
          />
        </div>
      </div>
    );
  };
  render() {
    return (
      <ModalC
        title={this.props.visible ? (this.state.info && this.state.info.isEdit ? this.showTitle() : '详情') : '交收'}
        visible={this.props.visible || this.props.visible1}
        maskClosable={false}
        footer={this.props.visible ? (this.state.info && this.state.info.isEdit ? undefined : null) : undefined}
        width={1200}
        onOk={this.props.visible ? (this.state.info && this.state.info.isEdit ? this.editClick : null) : this.orderInstruction}
        onCancel={this.onCancel}
        confirmLoading={this.props.loading.effects[this.getLoading()] === true}
      >
        {this.showTable()}
        {this.state.type == '19' && this.clinchTable()}
      </ModalC>
    );
  }
}
export default connect(({ bookkeepingSettle, newFundTrade, bondOfflineM, loading }) => ({ bookkeepingSettle, newFundTrade, bondOfflineM, loading }))(Detail);
